{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2c6154d4",
   "metadata": {},
   "source": [
    "# Tapered Transmission Lines\n",
    "\n",
    "A tapered line (or simply a taper), is an impedance transformer that matches an impedance $Z_1$ to an impedance $Z_2$ using a gradually varying characteristic impedance $Z(z)$ along the line. \n",
    "\n",
    "There is an infinite number of ways one can define a profile $Z(z)$ along the transmission line. scikit-rf implements a generic 1D Taper [Taper1D](../../api/taper.rst), from which the following direct classes are derived: `Linear`, `Exponential` and `SmoothStep`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5e81006",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import skrf as rf\n",
    "from skrf.media import MLine\n",
    "\n",
    "rf.stylely()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa11b39f",
   "metadata": {},
   "source": [
    "## Geometry"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d4f05ad",
   "metadata": {},
   "source": [
    "In this example, we compare a microstrip line which width $W$ is tapered and loaded with a 15 Ohm resistor:\n",
    "\n",
    "<img src=\"ANSYS_Circuit_reference.png\" width=\"1000\">\n",
    "\n",
    "Before comparing the taper profiles, let's define the other elements of the circuit, that is the piece of transmission lines and resistor:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc40cf5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Model Parameters\n",
    "freq = rf.Frequency(1, 20, unit='GHz', npoints=191)\n",
    "w1 = 20*rf.mil  # conductor width [m]\n",
    "w2 = 90*rf.mil  # conductor width [m]\n",
    "h = 20*rf.mil  # dielectric thickness [m]\n",
    "t = 0.7*rf.mil  # conductor thickness [m]\n",
    "rho = 1.724138e-8  # Copper resistivity [Ohm.m]\n",
    "ep_r = 10  # dielectric relative permittivity\n",
    "rough = 1e-6  # conductor RMS roughtness [m]\n",
    "taper_length = 200*rf.mil  # [m]\n",
    "\n",
    "# Media definitions\n",
    "microstrip_w1 = MLine(freq, w=w1, h=h, t=t, rho=rho, ep_r=ep_r, rough=rough,\n",
    "                      disp='kobayashi', z0_port=50)\n",
    "microstrip_w2 = MLine(freq, w=w2, h=h, t=t, rho=rho, ep_r=ep_r, rough=rough,\n",
    "                      disp='kobayashi', z0_port=50)\n",
    "\n",
    "# piece of transmission lines connected to the taper\n",
    "line1 = microstrip_w1.line(d=50, unit='mil', name='feeder')\n",
    "line2 = microstrip_w2.line(d=50, unit='mil', name='feeder')\n",
    "\n",
    "# loading resistor\n",
    "resistor = microstrip_w2.resistor(R=15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05faf152",
   "metadata": {},
   "source": [
    "The idea is hence to forge a transmission line of variable characteristic impedance. In this example, the width of the metallization $W$ is varied along the transmission line length. Most common profile are summarized in the figure below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e682143",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "z = np.linspace(0, taper_length)\n",
    "ax.plot(z, (w2-w1)/taper_length*z + w1, lw=2, label='linear')\n",
    "ax.plot(z, w1*np.exp(z/taper_length*(np.log(w2/w1))), lw=2, label='exponential')\n",
    "ax.set_xticks([0, taper_length])\n",
    "ax.set_xticklabels(['0', 'taper length'])\n",
    "ax.set_yticks([w1, w2])\n",
    "ax.set_yticklabels(['$W_1$', '$W_2$'])\n",
    "ax.legend()\n",
    "ax.set_title('Parameter profile along the taper length')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5ba96ad",
   "metadata": {},
   "source": [
    "### Linear Taper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0b721de",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a 2-port Network\n",
    "taper_linear = rf.taper.Linear(med=MLine, param='w', start=w1, stop=w2,\n",
    "                        length=taper_length, n_sections=50,\n",
    "                        med_kw={'frequency': freq, 'h': h, 't':t, 'ep_r': ep_r,\n",
    "                                'rough': rough, 'rho': rho}).network\n",
    "print(taper_linear)\n",
    "\n",
    "# build the full circuit\n",
    "# equivalent to ntwk = line1 ** taper_linear ** resistor ** line2 ** microstrip_w2.short()\n",
    "ntwk_linear = rf.cascade_list([line1, taper_linear, line2, resistor, microstrip_w2.short()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2a58407",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(ntwk_linear.frequency.f_scaled, ntwk_linear.s_mag[:,0], lw=2, label='scikit-rf - Linear')\n",
    "\n",
    "f_ref, s_mag_ref = np.loadtxt('ANSYS_Circuit_taper_linear_s_mag.csv', delimiter=',', skiprows=1, unpack=True)\n",
    "ax.plot(f_ref, s_mag_ref, label='ANSYS Circuit - Linear Taper', lw=2, ls='--')\n",
    "\n",
    "ax.set_xlabel('f [GHz]')\n",
    "ax.set_ylabel('$|s_{11}|$')\n",
    "ax.set_ylim(0, 0.6)\n",
    "ax.set_xlim(1, 20)\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e0eee9e",
   "metadata": {},
   "source": [
    "### Exponential Taper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d213ae64",
   "metadata": {},
   "outputs": [],
   "source": [
    "taper_exp = rf.taper.Exponential(med=MLine, param='w', start=w1, stop=w2,\n",
    "                        length=taper_length, n_sections=50,\n",
    "                        med_kw={'frequency': freq, 'h': h, 't':t, 'ep_r': ep_r,\n",
    "                                'rough': rough, 'rho': rho}).network\n",
    "\n",
    "ntwk_exp = line1 ** taper_exp ** line2 ** resistor ** microstrip_w2.short()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a67c366d",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(ntwk_exp.frequency.f_scaled, ntwk_exp.s_mag[:,0], lw=2, label='scikit-rf - Exponential')\n",
    "\n",
    "f_ref, s_mag_ref = np.loadtxt('ANSYS_Circuit_taper_exponential_s_mag.csv', delimiter=',', skiprows=1, unpack=True)\n",
    "ax.plot(f_ref, s_mag_ref, label='ANSYS Circuit - Exponential Taper', lw=2, ls='--')\n",
    "\n",
    "ax.set_xlabel('f [GHz]')\n",
    "ax.set_ylabel('$|s_{11}|$')\n",
    "ax.set_ylim(0, 0.6)\n",
    "ax.set_xlim(1, 20)\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a58c142",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
